using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;
using RgCore.GameData;

namespace RgCore.Commands.RgRACommands
{
    public class EditProfile : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ServerConsoleCommands, out _))
            {
                response = "你没有权限执行此命令！";
                return false;
            }


            if (arguments.Count < 4 || !ValueName.Contains(arguments.At(1).ToLower()) ||
                !ActionName.Contains(arguments.At(2).ToLower()))
            {
                response = "用法: /rg editprofile <玩家ID> <level|exp|mvp|points> <add|set> <数量>";
                return false;
            }

            if (!int.TryParse(arguments.At(0), out int id))
            {
                response = "请输入一个正确的ID数字";
                return false;
            }

            if (!int.TryParse(arguments.At(3), out int amount))
            {
                response = "请输入一个正确的数量数字";
                return false;
            }

            if (!Player.TryGet(id, out var tar))
            {
                response = "无法找到指定的玩家！";
                return false;
            }

            string method = string.Empty;
            switch (arguments.At(1).ToLower())
            {
                case "level":
                    method = "%Level";
                    break;
                case "exp":
                    method = "%Exp";
                    break;
                case "points":
                    method = "%Points";
                    break;
                case "mvp":
                    method = "%Mvp";
                    break;
            }

            if (arguments.At(2).ToLower() == "add")
                method = method.Replace("%", "Add");
            else
                method = method.Replace("%", "Set");
            GameplayData gd = GameplayData.GameDatas.FirstOrDefault(x => x.Player == tar);
            gd.GetType().GetMethod(method).Invoke(gd, new object[] { amount, true });
            response = "已完成操作";
            return true;
        }

        public string Command { get; } = "editprofile";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "各种对玩家数据的操作";
        public string[] ValueName { get; } = { "level", "exp", "mvp", "points" };
        public string[] ActionName { get; } = { "add", "set" };
    }
}